package com.wstro.controller.admin;

import com.baomidou.mybatisplus.plugins.Page;
import com.wstro.entity.MaterialEntity;
import com.wstro.service.MaterialService;
import com.wstro.util.PageUtils;
import com.wstro.util.R;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

@Controller
@RequestMapping("/admin/material")
public class MaterialController extends AbstractController {
    @Resource
    private MaterialService materialService;
    /**
     * 所有素材列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("admin:material:list")
    @ResponseBody
    public R list(Integer offset, Integer limit, String sort, String order,
                  @RequestParam(name = "search", required = false) String search) {
        String title = null;
        Map<String, String> searchList = parseObject(search, "title");
        if (null != searchList) {
            title = searchList.get("title");
        }
        offset = (offset / limit) + 1;
        Boolean flag = null; // 排序逻辑
        if (StringUtils.isNoneBlank(order)) {
            if (order.equalsIgnoreCase("asc")) {
                flag = true;
            } else {
                flag = false;
            }
        }
        Page<MaterialEntity> materialList = materialService.queryListByPage(offset, limit, title, sort, flag);
        PageUtils pageUtil = new PageUtils(materialList.getRecords(), materialList.getTotal(), materialList.getSize(),
                materialList.getCurrent());
        return R.ok().put("page", pageUtil);
    }
}
